<?php
class User{

    private $name;
    private $surname;
    private $login;
    private $password;
    private $email;

    public function register($Login,$Email,$Password,$Name,$Surname){
        $this-> login = $Login;
        $this-> email = $Email;
        $this-> password = $Password;
        $this-> name = $Name;
        $this-> surname = $Surname;

        $email = trim($this->email);
        $pass_md5 = md5($this-> password);

        $checkMail=mysql_query("SELECT * FROM users WHERE email='$this->email'");
        if(mysql_num_rows($checkMail)!=0){
            echo 'Podany email jest już w bazie danych.</br>';
        }
        else {
            $addQuery = "INSERT INTO `users` (`id`,`email`,`password`,`name`,`surname`,`login`) VALUES (NULL, '$this->email', '$pass_md5','$this->name','$this->surname','$this->login')";
            $idaddQuery = mysql_query($addQuery);
            if(!$idaddQuery){
                echo 'blad!' .  mysql_error();
            }
            else{
                echo 'Poprawnie się zarejestrowałeś. Możesz się zalogować <br>';
            }
        }
    }
    public function login($Login,$Password){
        $this-> login = $Login;
        $this-> password = $Password;
        $pass_md5 = md5($this->password);

        $login_query = "SELECT login, password FROM users WHERE login='$this->login' AND password='$pass_md5'";
        $result_login = mysql_query($login_query);
        $count_login = mysql_num_rows($result_login);

        if($count_login === 1){
            $session_query = "SELECT id,login,name,surname,email FROM users WHERE login='$this->login' AND password='$pass_md5'";
            $result_session = mysql_query($session_query);
            $session = mysql_fetch_object($result_session);

            $_SESSION['log'] = 'true';
            $_SESSION['id'] = $session-> id;
            $_SESSION['login'] = $session-> login;
            $_SESSION['name'] = $session->name;
            $_SESSION['surname'] = $session->surname;
            $_SESSION['email'] = $session->email;
            }
            else{
                echo "Logowanie nieudane";
             }
    }
    public function logout(){
        if($_SESSION['log']== 'true'){
            unset($_SESSION['log']);
            unset($_SESSION['id']);
            unset($_SESSION['login']);
            unset($_SESSION['name']);
            unset($_SESSION['surname']);
            unset($_SESSION['email']);
            session_destroy();
        }
    }
}
?>
